import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-04-24
 * Time: 16:26
 */
public class MyArrayStack {
    public int[] elem;
    public int usedSize;

    public MyArrayStack(){
        this.elem = new int[10];
    }

    public int push(int val){ // 压栈
        if (this.usedSize == this.elem.length){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[usedSize] = val;
        return elem[++usedSize];
    }

    public void pop(){ // 出栈  出栈的usedSize 与数组小标的对应
        if (usedSize == 0) {
            System.out.println("栈为空！");
            return;
        }
        int oldval = this.elem[usedSize-1];
        this.usedSize--;
        System.out.println(oldval);


    }

    public int peek(){ // 输出栈顶元素
        if (usedSize == 0){
            return -1;
        }
        return elem[usedSize-1];
    }

    public boolean empty(){
        if (usedSize == 0){
            return true;
        }else {
            return false;
        }
    }

}
